//
//  HYHomeListView.m
//  XPlayer
//
//  Created by liguoqing on 2023/9/5.
//

#import "HYHomeListView.h"
#import "HYHomeVideoListCell.h"
#import "HYHomeFolderListCell.h"

@interface HYHomeListView ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation HYHomeListView

- (instancetype)initWithViewModel:(HYHomeViewModel *)viewModel fileType:(HYHomeFileType)fileType
{
    self = [super init];
    if(self){
        self.viewModel = viewModel;
        self.fileType = fileType;
        [self configUI];
        [self bindSignal];
    }
    return self;
}

- (void)configUI
{
    [self addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

- (void)reloadList:(NSIndexPath *)indexPath
{
    if(indexPath){
        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    }else{
        [self.tableView reloadData];
    }
}

- (void)bindSignal
{
    @weakify(self);
    [self.viewModel.updatedContentSignal subscribeNext:^(id x) {
        @strongify(self);
        [self.tableView reloadData];
    }];
    [self.viewModel.reloadSubject subscribeNext:^(RACTuple *tuple) {
        @strongify(self);
        HYHomeFileType fileType = [tuple.first integerValue];
        NSIndexPath *mainIndexPath = tuple.second;
        NSIndexPath *subIndexPath = tuple.third;
        if(self.fileType == HYHomeFileType_All){
            [self reloadList:mainIndexPath];
        }else if(self.fileType == fileType && (fileType == HYHomeFileType_Video || fileType == HYHomeFileType_Audio || fileType == HYHomeFileType_File || fileType == HYHomeFileType_Pic || fileType == HYHomeFileType_Other)){
            [self reloadList:subIndexPath];
        }else if(self.fileType == HYHomeFileType_File && fileType == HYHomeFileType_Zip){
            [self reloadList:subIndexPath];
        }
    }];
}

#pragma mark - UITableViewDelegate && UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger row = [self.viewModel numberOfRowsIsHeader:NO fileType:self.fileType];
    tableView.hidden = !row;
    return row;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HYHomeFileType fileType = [self.viewModel fetchFileType:indexPath fileType:self.fileType];
    HYHomeBaseListCell *baseCell;
    if(fileType != HYHomeFileType_Video && fileType != HYHomeFileType_Audio){// 文件夹类型
        baseCell = [tableView dequeueReusableCellWithIdentifier:@"HYHomeFolderListCell" forIndexPath:indexPath];
    }else{// 视频类型
        baseCell = [tableView dequeueReusableCellWithIdentifier:@"HYHomeVideoListCell" forIndexPath:indexPath];
    }
    baseCell.setBtn.hidden = self.isSelectFile;
    [baseCell bindViewModel:[self.viewModel cellForRowAtIndexPath:indexPath isHeader:NO fileType:self.fileType]];
    return baseCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kItemListHeight+20;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.viewModel didSelectRowAtIndexPath:indexPath isHeader:NO fileType:self.fileType];
}

#pragma mark - loadLazy
- (UITableView *)tableView
{
    if(!_tableView){
        _tableView = [[UITableView alloc]init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [CommUtls dynamicColorWithLightModeHex:@"#F5F5F5" darkModeHex:@"#181A22"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.contentInset = UIEdgeInsetsMake(2.5, 0, 0, 0);
        [_tableView registerClass:[HYHomeVideoListCell class] forCellReuseIdentifier:@"HYHomeVideoListCell"];
        [_tableView registerClass:[HYHomeFolderListCell class] forCellReuseIdentifier:@"HYHomeFolderListCell"];
    }
    return _tableView;
}

@end
